﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace PlotDesigner
{
    public partial class DialogNode : UserControl
    {
        public string Speaker
        {
            set => btn_speaker.Text = value;
            get => btn_speaker.Text;
        }

        public string DlgValue
        {
            set => textBox_value.Text = value;
            get => textBox_value.Text;
        }

        public string DlgRtf;

        public bool Fold
        {
            set => checkBox_fold.Checked = value;
            get => checkBox_fold.Checked;
        }

        public DialogNode next = null;
        public DialogNode last = null;
        public PlotNode fa;
        public DialogNode(PlotNode pn)
        {
            InitializeComponent();
            fa = pn;
        }

        public List<DialogNode> getDlgList()
        {
            List<DialogNode> lst = new List<DialogNode>();
            DialogNode cur = this;
            while(cur != null)
            {
                lst.Add(cur);
                cur = cur.next;
            }
            return lst;
        }

        public XmlElement ToXml(XmlDocument doc)
        {
            List<DialogNode> dialogs = getDlgList();
            XmlElement xe_dlgs = doc.CreateElement("Dialogs");
            foreach(DialogNode dlg in dialogs)
            {
                XmlElement xe_dlg = doc.CreateElement("dialog");
                xe_dlg.SetAttribute("speaker", dlg.Speaker);
                XmlElement xe_value = doc.CreateElement("value");
                xe_value.InnerText = dlg.DlgValue;
                XmlElement xe_rtf = doc.CreateElement("rtf");
                xe_rtf.InnerText = dlg.DlgRtf;
                xe_dlg.AppendChild(xe_value);
                xe_dlg.AppendChild(xe_rtf);
                xe_dlgs.AppendChild(xe_dlg);
            }
            return xe_dlgs;
        }

        public void refreshId()
        {
            label_id.Text = (getIndex() + 1).ToString();
        }

        private void addDlg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogNode newDlg = new DialogNode(fa);
            if(this.next == null)
            {
                //这是最后一个对话
                newDlg.last = this;
                this.next = newDlg;
            }
            else
            {
                //插入对话
                newDlg.next = this.next;
                newDlg.last = this;
                this.next.last = newDlg;
                this.next = newDlg;
            }
            //Console.WriteLine("--------begin---------");
            //Console.WriteLine(newDlg.getIndex());
            //Console.WriteLine(newDlg.next?.getIndex());
            //Console.WriteLine("--------end-----------");
            fa.refreshDlg();
            Program.mainform.refreshPlot();
        }

        private void moveUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (last == null) return;//没有上一个，不能上移
            //.WriteLine("上移！");
            if (last.last == null)
            {
                //上一个节点是beginNode
                DialogNode up = new DialogNode(fa) { Speaker = this.Speaker, DlgValue = this.DlgValue };
                DialogNode down = new DialogNode(fa) { Speaker = fa.beginDlg.Speaker, DlgValue = fa.beginDlg.DlgValue };
                fa.beginDlg = up;
                up.next = down;
                up.last = null;
                down.next = this.next;
                if (down.next != null)
                    down.next.last = down;
                down.last = up;
            }
            else
            {
                //上一个节点是普通节点
                DialogNode down = null;
                DialogNode up = null;
                up = new DialogNode(fa) { Speaker = this.Speaker, DlgValue = this.DlgValue, last = last.last };//移动之后在上面的节点
                down = new DialogNode(fa) { Speaker = last.Speaker, DlgValue = last.DlgValue, last = up, next = next };//移动之后在下面的节点
                up.last.next = up;
                up.next = down;
                if (down.next != null)
                    down.next.last = down;
            }

            fa.refreshDlg();
        }

        private void moveDown_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (next == null) return;//没有下一个，不能下移
            if(last == null)
            {
                //第一个节点
                DialogNode up = new DialogNode(fa) { Speaker = next.Speaker, DlgValue = next.DlgValue };
                DialogNode down = new DialogNode(fa) { Speaker = fa.beginDlg.Speaker, DlgValue = fa.beginDlg.DlgValue };
                fa.beginDlg = up;
                up.next = down;
                up.last = null;
                down.next = this.next.next;
                if (down.next != null)
                    down.next.last = down;
                down.last = up;
            }
            else
            {
                //普通节点
                DialogNode down = new DialogNode(fa) { Speaker = this.Speaker, DlgValue = this.DlgValue };
                DialogNode up = new DialogNode(fa) { Speaker = next.Speaker, DlgValue = next.DlgValue };
                up.next = down;
                up.last = this.last;
                down.last = up;
                down.next = this.next.next;
                if (up.last != null)
                    up.last.next = up;
                if (down.next != null)
                    down.next.last = down;
            }
            
            fa.refreshDlg();
        }

        private void delDlg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (last == null) return;
            Console.WriteLine(123);
            last.next = this.next;
            fa.refreshDlg();
        }

        private void checkBox_fold_CheckedChanged(object sender, EventArgs e)
        {
            fa.refreshDlg();
        }

        public int getIndex()
        {
            int result = 0;
            DialogNode dn = this;
            //Console.WriteLine(dn.Speaker);
            while(dn.last != null)
            {
                result++;
                dn = dn.last;
                //Console.WriteLine(dn.Speaker);
            }
            return result;
        }

        private void edit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogEditor de = new DialogEditor(this);
            de.Show();
        }

        private void DialogNode_Load(object sender, EventArgs e)
        {
            label_id.Text = (getIndex() + 1).ToString();
        }

        private void doubleClick(object sender, EventArgs e)
        {
            Console.WriteLine(getIndex());
        }

        private void btn_speaker_Click(object sender, EventArgs e)
        {
            SpeakerEditor se = new SpeakerEditor(this);
            se.ShowDialog();
        }
    }
}
